package com.web.logindemo.service.permission;

import com.web.logindemo.common.util.SecurityUtils;
import com.web.logindemo.common.util.StringUtils;
import com.web.logindemo.domain.model.LoginUser;
import io.jsonwebtoken.lang.Collections;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 自定义权限实现
 *
 */
@Service("ss")
public class PermissionService {
    /**
     * 所有权限标识
     */
    private static final String ALL_PERMISSION = "*:*:*";

    /**
     * 管理员权限标识
     */
    private static final String SUPER_ADMIN = "admin";


    /**
     * 判断是否有某权限
     * @param permission
     * @return
     */
    public boolean hasPermi(String permission){
        if(StringUtils.isEmpty(permission)){
            return false;
        }
        LoginUser user = SecurityUtils.getLoginUser();
        if(StringUtils.isNull(user) || Collections.isEmpty(user.getPermissions())){
            return false;
        }
        return hasPermission(user.getPermissions(), permission);
    }

    private boolean hasPermission(Set<String> permissions, String permission) {
        return permissions.contains(ALL_PERMISSION) || permissions.contains(permission);
    }
}